package yygh.user.api;

import yygh.hospital.model.user.Patient;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yygh.common.result.Result;
import yygh.common.utils.AuthContextHolder;
import yygh.user.Service.PatientService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    PatientService patientService;

    //4.1 就诊人相关接口1：获取就诊人信息列表
    @ApiOperation(value = "获取就诊人列表")
    @GetMapping("/auth/findAll")
    public Result findAll(HttpServletRequest request){
        //首先根据request获取当前用户的编号
        Long userId = AuthContextHolder.getUserId(request);
       List<Patient> patients= patientService.findAllByUserId(userId);
        return  Result.ok(patients);
    }

    //4.2  就诊人相关接口2：根据id编号获取就诊人具体信息
    @ApiOperation(value = "获取就诊人信息")
    @GetMapping("/auth/get/{id}")
    public Result getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatientById(id);
        return  Result.ok(patient);
    }

    //4.3 就诊人相关接口3： 更新就诊人信息
    @ApiOperation(value = "更改救诊人信息")
    @PutMapping("/auth/update")
    public Result update(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return  Result.ok();
    }

    //4.4  就诊人相关接口4：根据id编号删除就诊人
    @ApiOperation(value = "删除用户的的救诊人")
    @DeleteMapping("/auth/remove/{id}")
    public Result deletePatient(@PathVariable Long id) {
        patientService.removeById(id);
        return  Result.ok();
    }

    //4.5 就诊人相关接口5：添加就诊人
    @ApiOperation(value = "添加用户")
    @PostMapping("/auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        //根据rquest获取对应的用户编号
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return  Result.ok();
    }


    /**
     * 内部接口，实现就诊人的信息获取，供其他微服务调用
     */
    //根据就诊人id获取就诊人信息
    @ApiOperation("内部接口，获取就诊人的信息")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id){
        Patient patient = patientService.getPatientById(id);
        return  patient;
    }



}
